home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / test / arctest.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  3.4 KB  |  139 lines

  1. /*
  2.  * @(#)ArcTest.java    1.9 95/09/01 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. import java.awt.*;
  20. import java.applet.*;
  21.  
  22. /**
  23.  * An interactive test of the Graphics.drawArc and Graphics.fillArc
  24.  * routines. Can be run either as a standalone application by
  25.  * typing "java ArcTest" or as an applet in the AppletViewer.
  26.  */
  27. public class ArcTest extends Applet {
  28.     ArcControls controls;
  29.     public void init() {
  30.     setLayout(new BorderLayout());
  31.     ArcCanvas c = new ArcCanvas();
  32.     add("Center", c);
  33.     add("South", controls = new ArcControls(c));
  34.     }
  35.  
  36.     public void start() {
  37.     controls.enable();
  38.     }
  39.  
  40.     public void stop() {
  41.     controls.disable();
  42.     }
  43.  
  44.     public boolean handleEvent(Event e) {
  45.     if (e.id == Event.WINDOW_DESTROY) {
  46.         System.exit(0);
  47.     }
  48.     return false;
  49.     }
  50.  
  51.     public static void main(String args[]) {
  52.     Frame f = new Frame("ArcTest");
  53.     ArcTest    arcTest = new ArcTest();
  54.  
  55.     arcTest.init();
  56.     arcTest.start();
  57.  
  58.     f.add("Center", arcTest);
  59.     f.resize(300, 300);
  60.     f.show();
  61.     }
  62. }
  63.     
  64.  
  65. class ArcCanvas extends Canvas {
  66.     int        startAngle = 0;
  67.     int        endAngle = 45;
  68.     boolean    filled = false;
  69.     Font    font;
  70.  
  71.     public void paint(Graphics g) {
  72.     Rectangle r = bounds();
  73.     int hlines = r.height / 10;
  74.     int vlines = r.width / 10;
  75.  
  76.     g.setColor(Color.pink);
  77.     for (int i = 1; i <= hlines; i++) {
  78.         g.drawLine(0, i * 10, r.width, i * 10);
  79.     }
  80.     for (int i = 1; i <= vlines; i++) {
  81.         g.drawLine(i * 10, 0, i * 10, r.height);
  82.     }
  83.  
  84.     g.setColor(Color.red);
  85.     if (filled) {
  86.         g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  87.     } else {
  88.         g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  89.     }
  90.  
  91.     g.setColor(Color.black);
  92.     g.setFont(font);
  93.     g.drawLine(0, r.height / 2, r.width, r.height / 2);
  94.     g.drawLine(r.width / 2, 0, r.width / 2, r.height);
  95.     g.drawLine(0, 0, r.width, r.height);
  96.     g.drawLine(r.width, 0, 0, r.height);
  97.     int sx = 10;
  98.     int sy = r.height - 28;
  99.     g.drawString("S = " + startAngle, sx, sy); 
  100.     g.drawString("E = " + endAngle, sx, sy + 14); 
  101.     }
  102.  
  103.     public void redraw(boolean filled, int start, int end) {
  104.     this.filled = filled;
  105.     this.startAngle = start;
  106.     this.endAngle = end;
  107.     repaint();
  108.     }
  109. }
  110.  
  111. class ArcControls extends Panel {
  112.     TextField s;
  113.     TextField e;
  114.     ArcCanvas canvas;
  115.  
  116.     public ArcControls(ArcCanvas canvas) {
  117.     this.canvas = canvas;
  118.     add(s = new TextField("0", 4));
  119.     add(e = new TextField("45", 4));
  120.     add(new Button("Fill"));
  121.     add(new Button("Draw"));
  122.     }
  123.  
  124.     public boolean action(Event ev, Object arg) {
  125.     if (ev.target instanceof Button) {
  126.         String label = (String)arg;
  127.  
  128.         canvas.redraw(label.equals("Fill"),
  129.               Integer.parseInt(s.getText().trim()),
  130.               Integer.parseInt(e.getText().trim()));
  131.  
  132.         return true;
  133.     }
  134.  
  135.     return false;
  136.     }
  137. }
  138.     
  139.